#include <iostream>
#include <windows.h>

// 简化版Unicode测试，不使用locale
typedef const wchar_t* (*AICoder_GenerateCodeW_func)(const wchar_t*);
typedef const wchar_t* (*AICoder_GetPluginInfoW_func)();
typedef bool (*AICoder_Initialize_func)();

// 简单的宽字符到多字节转换（避免locale依赖）
std::string WideToMultiByte(const std::wstring& wstr) {
    int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), NULL, 0, NULL, NULL);
    std::string str(size_needed, 0);
    WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), &str[0], size_needed, NULL, NULL);
    return str;
}

int main() {
    // 设置控制台为UTF-8输出
    SetConsoleOutputCP(65001);
    
    std::cout << "Testing AICoder Plugin (Unicode Version)..." << std::endl;
    
    HMODULE hDll = LoadLibrary("AICoder.dll");
    if (!hDll) {
        std::cout << "Failed to load AICoder.dll" << std::endl;
        return 1;
    }
    
    // 获取函数指针
    AICoder_Initialize_func initFunc = (AICoder_Initialize_func)GetProcAddress(hDll, "AICoder_Initialize");
    AICoder_GenerateCodeW_func genFuncW = (AICoder_GenerateCodeW_func)GetProcAddress(hDll, "AICoder_GenerateCode");
    AICoder_GetPluginInfoW_func infoFuncW = (AICoder_GetPluginInfoW_func)GetProcAddress(hDll, "AICoder_GetPluginInfo");
    
    if (!initFunc || !genFuncW || !infoFuncW) {
        std::cout << "Failed to get plugin functions" << std::endl;
        FreeLibrary(hDll);
        return 1;
    }
    
    std::cout << "AICoder Plugin loaded successfully" << std::endl;
    
    // 显示插件信息
    const wchar_t* pluginInfo = infoFuncW();
    std::string infoStr = WideToMultiByte(pluginInfo);
    std::cout << "Plugin Info: " << infoStr << std::endl;
    
    if (initFunc()) {
        std::cout << "Plugin initialized successfully" << std::endl;
        
        // 测试中文代码生成
        std::wstring requirement = L"创建一个Hello World程序";
        const wchar_t* code = genFuncW(requirement.c_str());
        
        std::string codeStr = WideToMultiByte(code);
        std::cout << "Generated Code:\n" << codeStr << std::endl;
        
        std::cout << "Plugin test completed successfully!" << std::endl;
    } else {
        std::cout << "Plugin initialization failed" << std::endl;
    }
    
    FreeLibrary(hDll);
    return 0;
}
